<?php
namespace App\Components;
use App\Components\Tools;
//加载七牛
use Qiniu\Auth;
use Qiniu\Config;
use Qiniu\Storage\UploadManager;
use Qiniu\Storage\BucketManager;
use Qiniu\Processing\PersistentFop;
/**
 * 七牛上传工具类
 */
class QiniuTools{
	function __construct() {
		$this->path="/".date('Y-m-d', time())."/";
		$this->pipeline = 'yks_wlys';
        $this->ak=Tools::getOneconfig("/config/third.json",array('qiniu','qnAccesskey'));
        $this->sk=Tools::getOneconfig("/config/third.json",array('qiniu','qnSecretkey'));
        $this->bucketName=Tools::getOneconfig("/config/third.json",array('qiniu','qnBucket'));
        $this->url=Tools::getOneconfig("/config/third.json",array('qiniu','qnUrl'));
	}
	
	//获取当前毫秒值精确到4位 生成MD5	type 是否生成MD5  默认 true
	function getMicrotime($type=true){
		if($type){
			return md5(microtime(true)*10000);
		}
		return microtime(true)*10000;
	}
	
	/**
	*	上传图片
	*	imgData		图片资源$_FILES['img']
	*	path		要保存到七牛的文件路径    默认 data/img
	*	imgType		图片类型
	*	typeName	取得类型用作后缀名
	*	imgContent	图片二进制
	*	imgName		图片路径+文件名+后缀
	*	oldImgPath	图片临时文件
	*/
	function upImg($imgData,$path="data/img"){
		$oldImgPath = isset($imgData['tmp_name']) && !empty($imgData['tmp_name']) ? $imgData['tmp_name'] : $this->returnJson(1,'图片不存在');
        $imgType = isset($imgData['type']) && !empty($imgData['type']) ? $imgData['type'] : $this->returnJson(1,'图片不存在');
		
        $typeName = explode('/',$imgType);
        $typeName = end($typeName);
        $imgName = $path . $this->path . $this->getMicrotime() . "." . $typeName;
        $imgContent = file_get_contents($oldImgPath);
        $auth = new Auth($this->ak,$this->sk);
        $token = $auth->uploadToken($this->bucketName);
        $uploadManager = new UploadManager();
        list($re,$error) = $uploadManager->put($token,$imgName,$imgContent);
        if(empty($error)){
            return $imgName;
        }
        return false;
	}
	
	/**
	*	上传视频
	*	videoData		视频资源$_FILES['video']
	*/
	function upVideo($videoData,$path="data/video"){
		$oldVideoPath = isset($videoData['tmp_name']) && !empty($videoData['tmp_name']) ? $videoData['tmp_name'] : $this->returnJson(1, '视频不存在');
        $videoType = isset($videoData['type']) && !empty($videoData['type']) ? $videoData['type'] : $this->returnJson(1, '视频不存在');
       
        $typeName = explode('/', $videoType);
        $typeName = end($typeName);
        $videoName = $path . $this->path . $this->getMicrotime() . "." . $typeName;
        $auth = new Auth($this->ak, $this->sk);
        $policy = array(
            'persistentPipeline' => $this->pipeline
        );
        $token = $auth->uploadToken($this->bucketName, null, 86400, $policy);
        $uploadManager = new UploadManager();
        list($re, $error) = $uploadManager->putFile($token, $videoName, $oldVideoPath);
        if (empty($error)) {
            return $videoName;
        }
        return false;
	}
	
	/**
	*	删除文件
	*	path	文件名
	*/
	function delFile($path){
		$path=str_replace($this->url."/","",$path);
		$auth = new Auth($this->ak,$this->sk);
		$BucketManager=new BucketManager($auth);
		$re=$BucketManager->delete($this->bucketName,$path);
		if(!$re){
			return true;
		}
		return false;
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}